<?php

namespace app\api\controller\baiying;


use app\common\exception\UploadException;
use think\Config;
use think\Hook;
use baiying\lib\BYTokenClient;
use think\Db;

/**
 * 公共接口
 */
class Byai extends Bybase {

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize() {
        parent::_initialize();
    }
    
    
    
    
    public function autoFenYeGetAiTask(){
        $uuid = md5(uniqid());
        $id = db('ec_ai_task_request')->insertGetId([
            'method'=>'ai_task',
            'uuid'=>$uuid,
            'createtime'=>time(),
        ]);
        $startDate = '2024-08-05';
        
        $currentPage = 1;
        while (true){
            $item_param = ['pageNum'=>$currentPage,'pageSize'=>20,'createDate'=>$startDate];
            $response = $this->getAiTaskList($item_param);
            if($response['code'] == 200){
                $this->taskInsert($response['data']['list']);

                db('ec_ai_task_request_page')->insert([
                    'ec_ai_task_request_id'=>$id,
                    'currentpage'=>$response['data']['pageNum'],
                    'config'=> json_encode($response,JSON_UNESCAPED_UNICODE),
                ]);
                
                $currentPage = $response['data']['pageNum'] + 1;
                if($currentPage > $response['data']['pages']){
                    $msg = '成功';
                    break;
                }
            }else{
                $msg = '失败';
                break;
            }
        }
        
        db('ec_ai_task_request')->where('id',$id)->update([
            'msg'=>$msg,
            'endtime'=>time()
        ]);
        
        $this->success('',$response);
        
    }
    
    
    public function ownGetAiTaskDetail(){
        $item_param = ['callJobId'=>'6845067'];
        $response = $this->getAiTaskDetail($item_param);
        if($response['code'] == 200){
            $this->taskUpdate($response['data']);
        }
        $this->success('',$response);
    }

    
    public function getAiTaskList($request_data) {
        
        $client = new BYTokenClient($this->access_token);

        $method = 'byai.openapi.calljob.list'; //要调用的api名称
        $api_version = '1.0.0'; //要调用的api版本号
        

        $my_params = ['companyId' => $this->companyId];
        $my_params = array_merge($my_params,$request_data);
        $my_files = [];

        $result = $client->post($method, $api_version, $my_params, $my_files);
        
        return $result;
        
    }
    
    
    public function getAiTaskDetail($request_data) {
        
        $client = new BYTokenClient($this->access_token);
        $method = 'byai.openapi.calljob.detail.get'; //要调用的api名称
        $api_version = '1.0.0'; //要调用的api版本号
        

        $my_params = ['companyId' => $this->companyId,'callJobId'=>$request_data['callJobId']];
        $my_files = [];

        $result = $client->post($method, $api_version, $my_params, $my_files);
        
        return $result;
        
    }
    
    
    public function taskInsert($data){
        if(empty($data)) return false;
        
        $fields_list = Db::getFields('fa_ec_ai_task');
        $fields = array_keys($fields_list);
        $paichu_fields = ['id','createtime','status'];
        $end_fields = array_diff($fields,$paichu_fields);
        
        $end_data = [];
        foreach ($data as $value){
            
            $is_exist = db('ec_ai_task')->where('callJobId',$value['callJobId'])->count();
            if(!empty($is_exist)) continue;
            $value['origin'] = 1;
            foreach ($end_fields as $v){
                $middle_data[$v] = empty($value[$v]) ? '' : $value[$v];
            }
            $middle_data['createtime'] = time();
            $middle_data['content'] = json_encode($value,JSON_UNESCAPED_UNICODE);
            
            
            $end_data[] = $middle_data;
            
        }
        
        if(empty($end_data)) return false;
        db('ec_ai_task')->insertAll($end_data);
    }
    
    
    public function taskUpdate($info){
        if(empty($info)) return false;
        
        $fields_list = Db::getFields('fa_ec_ai_task');
        $fields = array_keys($fields_list);
        $paichu_fields = ['id','createtime','callJobId','jobName','content'];
        $end_fields = array_diff($fields,$paichu_fields);
        
        
        $info['origin'] = 1;
        foreach ($end_fields as $v){
            $middle_data[$v] = empty($info[$v]) ? '' : $info[$v];
        }            
        $middle_data['content'] = json_encode($info,JSON_UNESCAPED_UNICODE);

        db('ec_ai_task')->where('callJobId',$info['callJobId'])->update($middle_data);
            
            
        
        
    }
    

}
